<template>
  <div class="app-container">
    <el-row :gutter="10" class="mb8">
      <el-col :span="10">
        <div style="color: red">向量数据库使用milvus</div>
      </el-col>
    </el-row>
    <el-form :model="queryParams" ref="queryForm" size="small" :inline="true" v-show="showSearch" label-width="68px">
      <el-form-item label="搜索内容" prop="searchValue">
        <el-input
          v-model="queryParams.searchValue"
          placeholder="请输入"
          clearable
          style="width: 240px"
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item>
        <el-button type="primary" icon="el-icon-search" size="mini" @click="handleQuery">搜索</el-button>
        <el-button icon="el-icon-refresh" size="mini" @click="resetQuery">重置</el-button>
      </el-form-item>
    </el-form>

    <el-row :gutter="10" class="mb8">
      <el-col :span="1.5">
        <el-button
          type="primary"
          plain
          icon="el-icon-plus"
          size="mini"
          @click="handleAdd"
        >新增</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="danger"
          plain
          icon="el-icon-delete"
          size="mini"
          :disabled="multiple"
          @click="handleDelete"
        >删除</el-button>
      </el-col>
<!--      <el-col :span="1.5">-->
<!--        <el-button-->
<!--          type="warning"-->
<!--          plain-->
<!--          icon="el-icon-download"-->
<!--          size="mini"-->
<!--          @click="handleExport"-->
<!--          v-hasPermi="['system:dict:export']"-->
<!--        >导出</el-button>-->
<!--      </el-col>-->
      <right-toolbar :showSearch.sync="showSearch" @queryTable="getList"></right-toolbar>
    </el-row>

    <el-table v-loading="loading" :data="mainList" @selection-change="handleSelectionChange">
      <el-table-column type="selection" width="55" align="center" />
      <el-table-column label="ID" align="center" prop="id" >
      <template slot-scope="scope">
        <router-link :to="'/ai/knowledge/doc/' + scope.row.id" class="link-type">
          <span>{{ scope.row.id }}</span>
        </router-link>
      </template>
      </el-table-column>
      <el-table-column label="名称" align="center" prop="name" >
        <template slot-scope="scope">
          <router-link :to="'/ai/knowledge/doc/' + scope.row.id" class="link-type">
            <span>{{ scope.row.name }}</span>
          </router-link>
        </template>
      </el-table-column>
      <el-table-column label="描述" align="center" prop="description" :show-overflow-tooltip="true" />
      <el-table-column label="维度" align="center" prop="dimensions" :show-overflow-tooltip="true" />
      <el-table-column label="稀疏模型" align="center" prop="enable_sparse">
        <template slot-scope="scope">
          <dict-tag :options="knowledgeEnableSparseOptions" :value="scope.row.enable_sparse"/>
        </template>
      </el-table-column>

      <el-table-column label="状态" align="center" prop="status">
        <template slot-scope="scope">
          <dict-tag :options="knowledgeStatusOptions" :value="scope.row.status"/>
        </template>
      </el-table-column>
<!--      <el-table-column label="备注" align="center" prop="remark" :show-overflow-tooltip="true" />-->
      <el-table-column label="创建时间" align="center" prop="created_at" width="180">
        <template slot-scope="scope">
          <span>{{ parseTime(scope.row.created_at) }}</span>
        </template>
      </el-table-column>
      <el-table-column label="更新时间" align="center" prop="updated_at" width="180">
        <template slot-scope="scope">
          <span>{{ parseTime(scope.row.updated_at) }}</span>
        </template>
      </el-table-column>
      <el-table-column label="操作" align="center" class-name="small-padding fixed-width">
        <template slot-scope="scope">
          <el-button
            size="mini"
            type="text"
            icon="el-icon-table"
            @click="handleInitMilvus(scope.row)"
          >初始化集合</el-button>
          <el-button
            size="mini"
            type="text"
            icon="el-icon-edit"
            @click="handleUpdate(scope.row)"
          >修改</el-button>
          <el-button
            size="mini"
            type="text"
            icon="el-icon-delete"
            @click="handleDelete(scope.row)"
          >删除</el-button>
        </template>
      </el-table-column>
    </el-table>

    <pagination
      v-show="total>0"
      :total="total"
      :page.sync="queryParams.pageNum"
      :limit.sync="queryParams.pageSize"
      @pagination="getList"
    />

    <!-- 添加或修改参数配置对话框 -->
    <el-dialog :title="title" :visible.sync="open" width="500px" append-to-body :close-on-click-modal="false">
      <el-form ref="form" :model="form" :rules="rules" label-width="80px">
<!--        <el-form-item label="状态" prop="status">-->
<!--          <el-radio-group v-model="form.status">-->
<!--            <el-radio-->
<!--              v-for="dict in dict.type.sys_normal_disable"-->
<!--              :key="dict.value"-->
<!--              :label="dict.value"-->
<!--            >{{dict.label}}</el-radio>-->
<!--          </el-radio-group>-->
<!--        </el-form-item>-->
        <el-form-item label="名称" prop="name">
          <el-input v-model="form.name" type="textarea" placeholder="请输入"></el-input>
        </el-form-item>
        <el-form-item label="描述" prop="description">
          <el-input v-model="form.description" type="textarea" placeholder="请输入"></el-input>
        </el-form-item>
        <el-form-item label="维度" prop="dimensions">
          <el-select v-model="form.dimensions" placeholder="请选择">
          <el-option
            v-for="item in knowledgeDimensionOptions"
            :key="item.label"
            :label="item.label"
            :value="item.value">
          </el-option>
          </el-select>
<!--          <el-input v-model="form.dimensions" type="number" placeholder="请输入"></el-input>-->
        </el-form-item>
        <el-form-item label="稀疏向量" prop="enable_sparse">
          <el-switch
            v-model="form.enable_sparse"
            active-color="#ff4949"
            inactive-color="#13ce66"
            :active-value="0"
            :inactive-value="1"
            active-text="禁用稀疏向量"
            inactive-text="启用稀疏向量">
          </el-switch>
        </el-form-item>

      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button type="primary" @click="submitForm">确 定</el-button>
        <el-button @click="cancel">取 消</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
import { addKnowledge, delKnowledge, getKnowledge, initMilvus, listKnowledge, updateKnowledge } from '@/api/knowledge'
import { knowledgeDimensionOptions,knowledgeStatusOptions,knowledgeEnableSparseOptions } from '@/utils/dicts'

export default {
  name: "Dict",
  // dicts: ['sys_normal_disable'],
  dicts: [],
  data() {
    return {
      // 遮罩层
      loading: true,
      // 选中数组
      ids: [],
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 显示搜索条件
      showSearch: true,
      // 总条数
      total: 0,
      // 字典表格数据
      mainList: [],
      // 弹出层标题
      title: "",
      // 是否显示弹出层
      open: false,
      knowledgeStatusOptions: knowledgeStatusOptions,
      knowledgeEnableSparseOptions: knowledgeEnableSparseOptions,
      knowledgeDimensionOptions: knowledgeDimensionOptions,
      // 日期范围
      dateRange: [],
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        searchValue: undefined,
      },
      // 表单参数
      form: {},
      // 表单校验
      rules: {
        name: [
          { required: true, message: "不能为空", trigger: "blur" }
        ],
        description: [
          { required: true, message: "不能为空", trigger: "blur" }
        ],
        dimensions: [
          { required: true, message: "不能为空", trigger: "blur" }
        ],
      }
    };
  },
  created() {
    this.getList();
  },
  methods: {
    /** 查询字典类型列表 */
    getList() {
      this.loading = true;

      listKnowledge(this.queryParams).then(response =>{
        this.mainList = response.rows;
        this.loading = false;
       console.log(response)
      }).catch(e=>{
        this.mainList = [];
        this.loading = false;
      })
      // listType(this.addDateRange(this.queryParams, this.dateRange)).then(response => { this.mainList = response.rows;
      //     this.total = response.total;
      //     this.loading = false;
      //   }
      // );
    },
    // 取消按钮
    cancel() {
      this.open = false;
      this.reset();
    },
    // 表单重置
    reset() {
      this.form = {
        id: undefined,
        created_at: undefined,
        description: undefined,
        dimensions: undefined,
        enable_sparse: undefined,
        name: "",
        updated_at: undefined,
        user_id: undefined,
      };
      this.resetForm("form");
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.pageNum = 1;
      this.getList();
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.dateRange = [];
      this.resetForm("queryForm");
      this.handleQuery();
    },
    /** 新增按钮操作 */
    handleAdd() {
      this.reset();
      this.form.dimensions=1024
      this.open = true;
      this.title = "添加知识库";
    },
    // 多选框选中数据
    handleSelectionChange(selection) {
      this.ids = selection.map(item => item.id)
      this.single = selection.length!=1
      this.multiple = !selection.length
    },
    handleInitMilvus(row){
      let param={
        kb_id: row.id
      }
      this.$modal.confirm('是否确认初始化ID为"' + row.id + '"的向量数据库集合？').then(function() {
        return initMilvus(param);
      }).then(() => {
        this.getList();
        this.$modal.msgSuccess("操作成功");
      }).catch(() => {});
    },
    /** 修改按钮操作 */
    handleUpdate(row) {
      this.reset();
      getKnowledge(row.id).then(response => {
        this.form = response.data;
        this.open = true;
        this.title = "修改知识库";
      });
    },
    /** 提交按钮 */
    submitForm: function() {
      this.$refs["form"].validate(valid => {
        if (valid) {
          if (this.form.id != undefined) {
            updateKnowledge(this.form).then(response => {
              this.$modal.msgSuccess("修改成功");
              this.open = false;
              this.getList();
            });
          } else {
            addKnowledge(this.form).then(response => {
              this.$modal.msgSuccess("新增成功");
              this.open = false;
              this.getList();
            });
          }
        }
      });
    },
    /** 删除按钮操作 */
    handleDelete(row) {
      console.log("row.id=",row.id)
      console.log("this.ids=",this.id)
      const ids = row.id?[row.id]:this.ids;
      this.$modal.confirm('是否确认删除编号为"' + ids + '"的数据项？').then(function() {
        return delKnowledge(ids);
      }).then(() => {
        this.getList();
        this.$modal.msgSuccess("删除成功");
      }).catch(() => {});
    },
  }
};
</script>
